﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Presentation.Commands;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;
using StockholmsStad.Runtime.Serialization.DataContract;

namespace StockholmsStad.Windows.UI.Modules.Maintenance.Models
{
    public class UserPresentationModel : INotifyPropertyChanged
    {
        public event EventHandler<DataEventArgs<UserPresentationModel>> Saved;
        public event EventHandler<DataEventArgs<UserPresentationModel>> Added;
        public event EventHandler<DataEventArgs<UserPresentationModel>> Deleted;
        public event EventHandler<DataEventArgs<UserPresentationModel>> Cancelled;
        public event EventHandler<DataEventArgs<UserPresentationModel>> ChangedPassword;

        public DelegateCommand<object> SaveCommand { get; private set; }
        public DelegateCommand<object> DeleteCommand { get; private set; }
        public DelegateCommand<object> AddCommand { get; private set; }
        public DelegateCommand<object> CancelCommand { get; private set; }
        public DelegateCommand<object> ChangePasswordCommand { get; private set; }

        public UserPresentationModel()
        {
            SaveCommand = new DelegateCommand<object>(Save);
            AddCommand = new DelegateCommand<object>(Add);
            DeleteCommand = new DelegateCommand<object>(Delete);
            CancelCommand = new DelegateCommand<object>(Cancel);
            ChangePasswordCommand = new DelegateCommand<object>(ChangePassword);

            PropertyChanged += OnPropertyChangedEvent;
        }

        #region Properties

        public string HeaderInfo
        {
            get { return HeaderNames.Users; }
        }

        public List<UserRole> UserRoles { get; set; }

        public int? ParvisUserId { get; set; }

        private string firstName;
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if (!value.Equals(firstName))
                {
                    firstName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
                }
            }
        }

        private string lastName;
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                if (!value.Equals(lastName))
                {
                    lastName = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
                }
            }
        }

        private string emailAddress;
        public string EmailAddress
        {
            get
            {
                return emailAddress;
            }
            set
            {
                if (!value.Equals(emailAddress))
                {
                    emailAddress = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("EmailAddress"));
                }
            }
        }

        private string phoneNumber;
        public string PhoneNumber
        {
            get
            {
                return phoneNumber;
            }
            set
            {
                if (!value.Equals(phoneNumber))
                {
                    phoneNumber = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("PhoneNumber"));
                }
            }
        }

        private string cellPhoneNumber;
        public string CellPhoneNumber
        {
            get
            {
                return cellPhoneNumber;
            }
            set
            {
                if (!value.Equals(cellPhoneNumber))
                {
                    cellPhoneNumber = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("CellPhoneNumber"));
                }
            }
        }

        private ValueIdPair userRole;
        public ValueIdPair UserRole
        {
            get
            {
                return userRole;
            }
            set
            {
                if (!value.Equals(userRole))
                {
                    userRole = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("UserRole"));
                }
            }
        }

        private bool isActive;
        public bool IsActive
        {
            get
            {
                return isActive;
            }
            set
            {
                if (!value.Equals(isActive))
                {
                    isActive = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("IsActive"));
                }
            }
        }

        #endregion

        private void OnPropertyChangedEvent(object sender, PropertyChangedEventArgs e)
        {
            SaveCommand.RaiseCanExecuteChanged();
        }

        private void Save(object obj)
        {
            OnSaved(new DataEventArgs<UserPresentationModel>(this));
        }

        private void Cancel(object obj)
        {
            OnCancelled(new DataEventArgs<UserPresentationModel>(this));
        }

        private void Add(object obj)
        {
            OnAdded(new DataEventArgs<UserPresentationModel>(this));
        }

        private void Delete(object obj)
        {
            OnDeleted(new DataEventArgs<UserPresentationModel>(this));
        }

        private void ChangePassword(object obj)
        {
            OnChangedPassword(new DataEventArgs<UserPresentationModel>(this));
        }

        private void OnSaved(DataEventArgs<UserPresentationModel> e)
        {
            var savedHandler = Saved;

            if (savedHandler != null)
                savedHandler(this, e);
        }

        private void OnAdded(DataEventArgs<UserPresentationModel> e)
        {
            var addedHandler = Added;

            if (addedHandler != null)
                addedHandler(this, e);
        }

        private void OnDeleted(DataEventArgs<UserPresentationModel> e)
        {
            var deleteHandler = Deleted;

            if (deleteHandler != null)
                deleteHandler(this, e);
        }

        private void OnCancelled(DataEventArgs<UserPresentationModel> e)
        {
            var cancelHandler = Cancelled;

            if (cancelHandler != null)
                cancelHandler(this, e);
        }

        private void OnChangedPassword(DataEventArgs<UserPresentationModel> e)
        {
            var changedHandler = ChangedPassword;

            if (changedHandler != null)
                changedHandler(this, e);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
